<script setup>
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import request from '../../api/request'

const router = useRouter()
const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const formRef = ref()

const handleLogin = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    const res = await request({
      url: '/admin/login',
      method: 'post',
      data:loginForm
    })
    if (res.data) {
      localStorage.setItem('token', res.data)
      ElMessage.success('登录成功')
      router.push('/manager/home')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败，请重试')
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-left">
      <div class="login-info">
        <h2>管理员登录</h2>
        <p>请使用管理员账号登录系统</p>
      </div>
    </div>
    
    <div class="login-right">
      <div class="login-form-container">
        <h1 class="login-title">管理员登录</h1>
        
        <el-form
          ref="formRef"
          :model="loginForm"
          :rules="rules"
          class="login-form"
        >
          <!-- 用户名输入框 -->
          <el-form-item>
            <el-input
              v-model="loginForm.username"
              placeholder="管理员用户名"
              :prefix-icon="User"
            />
          </el-form-item>
          
          <!-- 密码输入框 -->
          <el-form-item>
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              :prefix-icon="Lock"
            />
          </el-form-item>
          
          <!-- 登录按钮 -->
          <el-form-item>
            <el-button
              type="primary"
              class="login-button"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  min-height: 90vh;
  width: 80%;
  max-width: 1200px;
  margin: 0 auto;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.login-left {
  flex: 1;
  background-color: #67C23A;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, rgba(103, 194, 58, 0.8) 0%, rgba(103, 194, 58, 0.9) 100%);
  border-radius: 0 0 50% 0;
}

.login-info {
  max-width: 400px;
  padding: 40px;
  position: relative;
  z-index: 1;
}

.login-info h2 {
  font-size: 2.5rem;
  margin-bottom: 20px;
}

.login-info p {
  margin-bottom: 30px;
  line-height: 1.6;
}

.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}

.login-form-container {
  width: 100%;
  max-width: 400px;
}

.login-title {
  text-align: center;
  font-size: 2rem;
  margin-bottom: 40px;
  color: #67C23A;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  border-radius: 4px;
  background-color: #67C23A;
  border-color: #67C23A;
}

.login-button:hover {
  background-color: #85ce61;
  border-color: #85ce61;
}
</style>